//
//  SearchResultDetailViewController.m
//  BVCMS Search
//
//  Created by Allen Thomas on 9/13/10.
//

#import "SearchResultDetailViewController.h"
#import "ImageViewController.h"

@implementation SearchResultDetailViewController

@synthesize person;
@synthesize table;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	UIImage *background = [UIImage imageNamed: @"background.png"];    
	UIImageView *imageView = [[UIImageView alloc] initWithImage: background]; 
	
	[self.view addSubview: imageView];
	[self.view sendSubviewToBack: imageView];
	[imageView release];
	
	table.backgroundColor = [UIColor clearColor];
	
    [super viewDidLoad];
}

#pragma mark Table Methods

/*
 Triggered when the user selects a row.  In this case, we get the row type, and then do the specific action.
 */
- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

	if (indexPath.section == 0 && indexPath.row == 0) {
		if( [[person valueForKey:@"haspicture"] isEqualToString:@"1"] ) {
			// Load search form
			ImageViewController	*imageView = [[ImageViewController alloc]
													initWithNibName:@"ImageView" bundle:[NSBundle mainBundle]];
			
			imageView.title = @"Member Photo";
			imageView.peopleid = [person valueForKey:@"peopleid"];
			
			[self.navigationController pushViewController:imageView animated:YES];

			[imageView release];
		}
		
	}
	
	if (indexPath.section == 1 ) {
		
		if(indexPath.row == 0) {
			UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
			UITableViewCell *othercell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
	
			NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@,%@", cell.detailTextLabel.text,othercell.detailTextLabel.text];
			urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];
		}
		
		if (indexPath.row == 1) {
			UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

			NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", cell.detailTextLabel.text];
			urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];
		}
			
		if (indexPath.row > 1 && indexPath.row < 5) {
			UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

			NSString *trimmedPhoneNumber = [[cell.detailTextLabel.text componentsSeparatedByCharactersInSet:
											[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
											componentsJoinedByString:@""];
			NSString *urlString = [NSString stringWithFormat:@"tel://%@", trimmedPhoneNumber];
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
		}
		if(indexPath.row == 5) {
			UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

			NSString *urlString = [NSString stringWithFormat:@"mailto:%@", cell.detailTextLabel.text];
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
		}
		
	}
	
	return nil;
}

/*
 Return number of sections.
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

/*
 Return the title of a section.
 */
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
	switch (section) {
		case 0:
			return @"Member Info";
			break;
		case 1:
			return @"Contact Info";
			break;
	}
	return 0;
}

/*
 Return number of rows in a section.
 */
- (NSInteger)tableView:(UITableView *)table
 numberOfRowsInSection:(NSInteger)section {
	switch (section) {
		case 0:
			return 4;
			break;
		case 1:
			return 6;
			break;
	}
	return 0;
}

/*
 Return a UITableViewCell for a row.  Some rows will have different accessory indicators.
 */
- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	
	
	UITableViewCell * cell = [tableView
							  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
	
	if(cell == nil) {
		
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleValue1
				 reuseIdentifier:SimpleTableIdentifier] autorelease];
	}
	
	cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
	cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:13.0];
	cell.detailTextLabel.minimumFontSize = 10;
	cell.accessoryView = nil;
	
	switch (indexPath.section) {
		case 0:
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			
			switch (indexPath.row) {
				case 0:
					cell.textLabel.text = @"Name";
					cell.detailTextLabel.text = [person valueForKey:@"name"];
					
					NSString *hasPicture = [person valueForKey:@"haspicture"];
					if ( [hasPicture isEqualToString:@"1"] ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"pic.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
						
						cell.selectionStyle = UITableViewCellSelectionStyleBlue;
					}
					break;
				case 1:
					cell.textLabel.text = @"Age";
					cell.detailTextLabel.text = [person valueForKey:@"age"];
					break;
				case 2:
					cell.textLabel.text = @"Birth Date";
					cell.detailTextLabel.text = [person valueForKey:@"birthdate"];
					break;
				case 3:
					cell.textLabel.text = @"Member Status";
					cell.detailTextLabel.text = [person valueForKey:@"memberstatus"];
					break;
			}
			break;
		case 1:
						
			switch (indexPath.row) {
				case 0:
					cell.textLabel.text = @"Address";
					cell.detailTextLabel.text = [person valueForKey:@"address"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"map.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;
				case 1:
					cell.textLabel.text = @"City/State/Zip";
					cell.detailTextLabel.text = [person valueForKey:@"citystatezip"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"map.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;					
				case 2:
					cell.textLabel.text = @"Home Phone";
					cell.detailTextLabel.text = [person valueForKey:@"homephone"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"phone.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;
				case 3:
					cell.textLabel.text = @"Cell Phone";
					cell.detailTextLabel.text = [person valueForKey:@"cellphone"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"phone.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;
				case 4:
					cell.textLabel.text = @"Work Phone";
					cell.detailTextLabel.text = [person valueForKey:@"workphone"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"phone.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;
				case 5:
					cell.textLabel.text = @"Email";
					cell.detailTextLabel.text = [person valueForKey:@"email"];
					if( [cell.detailTextLabel.text length] > 0 ) {
						UIImageView *accImage = [[ UIImageView alloc ] 
												 initWithImage:[UIImage imageNamed:@"mail.png" ]];
						cell.accessoryView = accImage;
						[accImage release];
					}
					break;
			}
			
			if( [cell.detailTextLabel.text length] > 0 ) 
				cell.selectionStyle = UITableViewCellSelectionStyleNone;			
			
			break;			
			
	}
		
	return cell;
}


/* 
 Release allocated memory.
 */
- (void)dealloc {
	[person release];
	[table release];
    [super dealloc];
}




@end
